﻿using System;
using System.Web;
using System.Data;
using System.Linq;
using System.Web.UI;
using System.Xml.Linq;
using System.Collections;
using System.Web.Security;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BLL;
using DLL;
using ENUMS;

public partial class bjPage : System.Web.UI.Page
{
    //保存用户登陆信息
    private static landInfo landInfo = new landInfo();

    // 画面初始化
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["landInfo"] == null)
            return;
        else
        {
            //获取登陆信息
            landInfo = (landInfo)Session["landInfo"];
            //验证登陆者身份
            if (landInfo.Ident != identity.manager)
                dropFDY.Enabled = false;
            else
                dropFDY.Enabled = true;
        }

        if (!IsPostBack)
        {
            //GridView绑定数据源
            BindToGridView();
            //DropDownList绑定数据源
            BindYX();
            //绑定员工数据
            BindYG();
        }
        viewIsNoData();
    }

    //获取已有班级数据信息
    private void BindToGridView()
    {
        if (landInfo.Ident == identity.manager)
        {
            gvBJ.DataSource = bj_BLL.GetBJSource();
            gvBJ.DataBind();
        }
        else
        {
            gvBJ.DataSource = bj_BLL.GetBJSource(landInfo.UserId);
            gvBJ.DataBind();
        }
    }

    //绑定院系下拉列表项
    private void BindYX()
    {
        dropYX.DataSource = yx_BLL.GetSource();
        dropYX.DataTextField = "yxName";
        dropYX.DataValueField = "yxNum";
        dropYX.DataBind();
        dropYX_SelectedIndexChanged(null, null);
    }

    //根据所选院系获取相关专业
    protected void dropYX_SelectedIndexChanged(object sender, EventArgs e)
    {
        dropZY.DataSource = zy_BLL.GetSource(dropYX.SelectedValue);
        dropZY.DataTextField = "zyName";
        dropZY.DataValueField = "zyNum";
        dropZY.DataBind();
    }

    //获取员工信息
    private void BindYG()
    {
        dropFDY.DataSource = ry_BLL.GetSource();
        dropFDY.DataTextField = "zgName";
        dropFDY.DataValueField = "zgId";
        dropFDY.DataBind();
        dropFDY.ClearSelection();
        dropFDY.Items.FindByText(landInfo.UserName).Selected = true;
    }

    // 换页
    protected void gvBJ_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvBJ.PageIndex = e.NewPageIndex;
        BindToGridView();
    }

    #region 脚本
    /// <summary>
    /// 显示报消息窗口
    /// </summary>
    /// <param name="Msg"></param>
    private void MsgBoxShow(string Msg)
    {
        RegisterStartupScript("Msg", "<script type='text/javascript' language='javascript'>alert('" + Msg + "')</script>");
    }
    /// <summary>
    /// 刷新当前页
    /// </summary>
    private void RefurPage()
    {
        RegisterStartupScript("refur", "<script type='text/javascript'>location.replace(location.href)</script>");
    }
    /// <summary>
    /// 如果GridView中没有数据则显示提示信息
    /// </summary>
    protected void viewIsNoData()
    {
        if (gvBJ.Rows.Count == 0)
        {
            RegisterStartupScript("prompt1", "<script type='text/javascript'>document.getElementById('divBrowse').innerHTML='(尚未找到相关数据)'</script>");
        }
    }
    #endregion

    // 删除gvBJ中行
    protected void gvBJ_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string Delete_ID = gvBJ.DataKeys[e.RowIndex][0].ToString();
        bool bl = bj_BLL.DeleteBJ(Delete_ID);
        if (bl)
        {
            BindToGridView();
            MsgBoxShow("成功删除信息！");
        }
        else
        {
            MsgBoxShow("删除信息失败！");
        }
    }

    // 点击" 添加
    protected void btnRefur_Click(object sender, EventArgs e)
    {
        BJ newBJ = new BJ();
        newBJ.zyNum = decimal.Parse(dropZY.SelectedValue);
        newBJ.bjName = txtBJName.Text;
        newBJ.bjZF = chkZF.Checked;
        newBJ.zgId = decimal.Parse(dropFDY.SelectedValue);
        if (btnRefur.Text == "添加")
        {
            bool bl = bj_BLL.AddnewBJ(newBJ);
            if (bl)
            {
                MsgBoxShow("添加成功！");
                if (gvBJ.Rows.Count == 0)
                    RefurPage();
                else
                {
                    BindToGridView();
                    btnCancel_Click(sender, e);
                }               
            }
            else
            {
                MsgBoxShow("添加失败！");
            }
        }
        else
        {
            bool b = bj_BLL.UpdateBJ(seleBJNum, newBJ);
            if (b)
            {
                MsgBoxShow("更新成功！");
                BindToGridView();
                btnCancel_Click(sender, e);
            }
            else
            {
                MsgBoxShow("更新失败！");
                btnCancel_Click(sender, e);
            }
        }
    }

    //选择班级
    private static string seleBJNum = "";
    protected void gvBJ_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        seleBJNum = gvBJ.DataKeys[e.NewSelectedIndex][0].ToString();
        GridViewRow row = gvBJ.Rows[e.NewSelectedIndex];
        dropYX.ClearSelection();
        dropZY.ClearSelection();
        dropFDY.ClearSelection();
        dropYX.Items.FindByText(row.Cells[0].Text).Selected = true;
        //根据院系锁定专业
        dropYX_SelectedIndexChanged(sender, e);
        dropZY.Items.FindByText(row.Cells[1].Text).Selected = true;
        dropFDY.Items.FindByText(row.Cells[3].Text).Selected = true;
        txtBJName.Text =((HyperLink) row.Cells[2].Controls[0]).Text;
        chkZF.Checked = ((CheckBox)row.Cells[4].Controls[0]).Checked;
        btnRefur.Text = "更新";
    }

   //取消
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        dropYX.ClearSelection();
        dropZY.ClearSelection();
        dropYX_SelectedIndexChanged(sender, e);
        txtBJName.Text = "";
        chkZF.Checked = false;
        btnRefur.Text = "添加";
    }
}
